<script lang="ts" setup name="Goods">
import useStore from '@/store'
import { storeToRefs } from 'pinia'
import { ref, watchEffect } from 'vue'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import Message from '@/components/message'
const route = useRoute()
const { goods, cart } = useStore()
const { info } = storeToRefs(goods)
watchEffect(() => {
  const id = route.params.id
  if (id && route.fullPath === `/product/${id}`) {
    // 先清空商品的信息
    goods.resetGoodsInfo()
    // 再去获取商品的信息
    goods.getGoodsInfo(id as string)
  }
})
let skuIdParam = ''
const changeGoods = (skuId: string) => {
  console.log(skuId);
  skuIdParam = skuId
  // 父组件接收到skuId 去skus列表中查找 更新商品的现价 原价 库存
  const sku = info.value.skus.find(item => item.id === skuId)
  // console.log(sku)
  if (sku) {
    info.value.price = sku.price
    info.value.oldPrice = sku.oldPrice
    info.value.inventory = sku.inventory
  }
}
const count = ref(1)
const change = (num: number) => {
  // console.log('最新的计数器内容是' + num);
  // console.log('最新的计数器内容是' + count.value);
}
// 加入购物车
const addCart = async () => {
  // 判断子传父传递的skuId有没有值
  if (!skuIdParam) return Message.warning('请选择完整的商品规格')
  // console.log(skuIdParam, info)
  // 发送请求加入购物车
  // console.log('加入购物车成功', count.value)
  // await cart.addCart(skuIdParam, newNum)
  // await cart.addCart(skuIdParam, count.value)
  // 处理 添加商品的attrsText字段 
  // 1. 拿着skuId去info.skus找 对应的sku
  const sku = info.value.skus.find(item => item.id === skuIdParam)
  // console.log(sku)
  // 2. 遍历sku的specs 拼接一个字符串
  const arr = sku?.specs.map(item => {
    return `${item.name}: ${item.valueName}`
  })
  await cart.addCart({
    // 为了本地添加展示数据
    id: info.value.id, // 商品id
    name: info.value.name, // 商品的名字
    picture: info.value.mainPictures[0], // 商品图片
    price: info.value.price, // 商品原价格
    count: count.value, // 商品数量
    skuId: skuIdParam, // 商品skuId
    attrsText: arr?.join(' ')!, // 规格描述
    selected: true, // 是否选中
    nowPrice: info.value.price, // 商品现价
    stock: info.value.inventory, // 商品库存
    isEffective: true, // 商品是否有效
  })
  Message.success('加入购物车成功')
  skuIdParam = ''  // 解决跳到下一个商品可以直接加入购物车的bug
}
</script>
<template>
  <div class="xtx-goods-page">
    <div class="container">
      <!-- 面包屑 
      info 默认是空对象, 但是dom渲染不会等到请求回来后再渲染
      v-if 判断 info.某个属性
      -->
      <Transition name="fade">
        <XtxBread v-if="info.id">
          <XtxBreadItem to="/">首页</XtxBreadItem>
          <XtxBreadItem :to="`/category/${info.categories[1].id}`">
            {{ info.categories[1].name }}
          </XtxBreadItem>
          <XtxBreadItem :to="`/category/sub/${info.categories[0].id}`">
            {{ info.categories[0].name }}
          </XtxBreadItem>
          <XtxBreadItem>{{ info.name }}</XtxBreadItem>
        </XtxBread>
        <XtxBread v-else>
          <XtxBreadItem to="/">首页</XtxBreadItem>
          <XtxBreadItem></XtxBreadItem>
        </XtxBread>
      </Transition>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 左侧 -->
        <div class="media">
          <!-- 放大镜图片预览组件 -->
          <GoodsImage :images="info.mainPictures"></GoodsImage>
          <!-- 商品销量 评价 组件 GoodsSales -->
          <GoodsSales></GoodsSales>
        </div>
        <!-- 右侧 -->
        <div class="spec">
          <!-- GoodsName组件 -->
          <GoodsName :goods="info"></GoodsName>
          <!-- GoodsSku组件 -->
          <!-- 写死的父传子skuId -->
          <!-- <GoodsSku :goods="info" v-if="info.id" skuId="1369155870306340866" @changeGoods="changeGoods"></GoodsSku> -->
          <!-- 找库存最多的 或者 价格最好的  作业-->
          <!-- <GoodsSku :goods="info" :key="info.id" v-if="info.id" skuId="1369155870306340866" @changeGoods="changeGoods"> -->
          <GoodsSku :goods="info" v-if="info.id" skuId="1369155870306340866" @changeGoods="changeGoods">
          </GoodsSku>
          <!-- 计数器组件 -->
          <XtxNumbox v-model="count" :min="1" :max="10" label="数量" @change="change"></XtxNumbox>
          <!-- 加入购物车按钮 -->
          <XtxButton size="middle" type="primary" style="margin-top: 20px" @click="addCart">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsDetail v-if="info.id" :goods="info" />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <!-- 24小时 -->
          <GoodsHot :type="1" />
          <!-- 周热销榜 -->
          <GoodsHot :type="2" />
          <!-- 总排行 -->
          <GoodsHot :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }

  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

.goods-footer {
  display: flex;
  margin-top: 20px;

  .goods-article {
    width: 940px;
    margin-right: 20px;
  }

  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}

.goods-tabs {
  min-height: 600px;
  background: #fff;
}

.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
